import './css/weui.css'; 
import './css/public.css';  
import LocalStorage from './common/localStorage.module.js' ;
import common from './common/common.js'; 

Date.prototype.format = common.format;

let _localStorage = new LocalStorage.LocalStorage();   

function addComment(data) {
	let commentItem = `<div class="weui-media-box weui-media-box_text">
			                    <h4 class="weui-media-box__title">${data.userNickname}&nbsp;Say:</h4>
			                    <p class="weui-media-box__desc">${data.userComContent}</p>
			                    <ul class="weui-media-box__info">
			                        <li class="weui-media-box__info__meta"></li>
			                        <li class="weui-media-box__info__meta">${data.dateTIme}</li>
			                        <li class="weui-media-box__info__meta weui-media-box__info__meta_extra">${data.userWebsite}</li>
			                    </ul>
		                    </div>`
	$("#commentsList").append(commentItem); 
}


$(function(){
	$("#headerTitle").text("Comment"); 
	$("#submitButton").on("touchstart", function (e) {   
		if (  Object.keys(_localStorage.data).length === 0  ) {   
			let localData = { comments : [] };
			localData.comments.push({
				userNickname : $("#userNickname").val(),
				userWebsite : $("#userWebsite").val(),
				userComContent : $("#userComContent").val(),
				dateTIme : (new Date).format('yyyy-MM-dd h:m:s')
			});  
			_localStorage.data = localData; 
		} else { 
			let localData = _localStorage.data;
			localData.comments.push({
				userNickname : $("#userNickname").val(),
				userWebsite : $("#userWebsite").val(),
				userComContent : $("#userComContent").val(),
				dateTIme : (new Date).format('yyyy-MM-dd h:m:s')
			}); 
			_localStorage.data = localData; 
		}
		console.log(_localStorage.data.comments);
		addComment(_localStorage.data.comments[_localStorage.data.comments.length - 1]);
	}); 
	if (  Object.keys(_localStorage.data).length !== 0  ) { 
		_localStorage.data.comments.forEach(data => addComment(data) );
	}
});
